
var user_data = {
    username: "%s",
    password: "%s",
    autosubmit_login : "%s"
};


var loginModule = (function(){

    var SIGNIN_LABELS = ['sign-in','log-in','signon','sign on','login', 'log in', 'login_in', 'signin','sign in', 'sign_in', 'log on', 'log_on', 'logon','登  录','登录','Inloggen','einloggen','giriş','zaloguj','लॉग इन करें','valider','accedi','anmelden','weiter','accès','вход','kirjaudu','zaloguj się','let me in','credentials','authentication','authenticate'];
    var SIGNIN_LABELS_SECONDRY = ['next','ok','submit','enter','entrar','continue','connexion','sisenen','toliau','далее'];
    var SIGNUP_LABELS = ['signup', 'sign_up', 'sign up', 'register', 'create', 'reg', 'join', 'start', 'free', 'trial'];
    var fixVisible = ["www.tumblr.com"];
    var EXCLUDE_LINKS = ["forgot","remember me","new user",'view',"register","vergessen"];
    var EXCLUDE_FORM = ["creditcard","dc-form","cc-form","creditcard-form","debitcard-form","creditcard-payment-form","debitcard-payment-form","payment"];

    var ArrayProto = Array.prototype,slice = ArrayProto.slice;

    function getStyle(el, css){
        var style = null;
        if(window.getComputedStyle) {
            style = window.getComputedStyle(el,null);
            if(style) {
                return style[css];
            }
        }

        if(el.currentStyle) {
            style = el.currentStyle;
            if(style) {
                return style[css];
            }
        }

        return null;
    }

    function triggerEvent(element,event) {
        var evt = document.createEvent("HTMLEvents");
        evt.initEvent(event, true, true);
        if(element) {
            element.dispatchEvent(evt);
        }
    }

    function triggerKeyboardEvent(field,keyEvent) {
        var evt = field.ownerDocument.createEvent('KeyboardEvent');

        if (evt && evt.initKeyboardEvent) {
            evt.initKeyboardEvent(keyEvent,true,true);
        }

        if (evt && evt.initKeyEvent) {
            evt.initKeyEvent(keyEvent, true, true,null,false, false, false, false,0,0);
        }

        field.dispatchEvent(evt);
    }

    function simulateClickEvent(field) {
      if (!field || field && 'function' !== typeof field.click){return false;}
      field.click();
      return true;
    }

    function simulateKeyPress(field) {
      simulateClickEvent(field);
      field.focus();
      triggerKeyboardEvent(field, 'keydown');
      triggerKeyboardEvent(field, 'keyup');
      triggerKeyboardEvent(field, 'keypress');
    }

    function simulateKeyBoardEvent(field) {
        var evt = field.ownerDocument.createEvent('HTMLEvents');
        var evt1 = field.ownerDocument.createEvent('HTMLEvents');

        field.focus();
        triggerKeyboardEvent(field,'keydown');
        triggerKeyboardEvent(field,'keyup');
        triggerKeyboardEvent(field,'keypress');

        evt.initEvent('input', !0, !0);
        field.dispatchEvent(evt);

        evt1.initEvent('change', !0, !0);
        field.dispatchEvent(evt1);

        field.blur();
    }

    function triggerSubmitEvent(form) {
         if (!form) {return false;}
         var evt = document.createEvent('UIEvent');
         evt.initEvent('submit', !0, !0, window, 1);
         form.dispatchEvent(evt);
         try {
           form.submit();
         } catch(err) {}

         return true;
    }

    function isVisible(el,isDeep) {
        if(!el) {return false;}

        var original_el = el,elIsVisible = true,elementStyle = el.style,elementNode = el.nodeName.toLowerCase();

        if('undefined' === typeof isDeep) {
            isDeep = true;
        }

        if(elementNode === 'form') {
            elIsVisible = elIsVisible && (elementStyle.display !== 'none') && ((elementStyle.visibility !== "hidden")) && getStyle(el,'opacity') === "1";
        } else {
            elIsVisible = elIsVisible && el.offsetWidth > 0 && el.offsetHeight > 0 && 'hidden' !== getStyle(el,'visibility');
        }

        if(isDeep && elIsVisible) {
            while('body' !== el.tagName.toLowerCase() && elIsVisible) {
                el = el.parentElement;
                elIsVisible = elIsVisible && 'none' !== getStyle(el,'display') && "0" !== getStyle(el,'opacity')&& ("-9999px" !== getStyle(el,'left'));
            }
        }

        return elIsVisible;
    }

    function isZindexHigher(el) {
        var isZindexHigher = false;
        while('body' !== el.tagName.toLowerCase() && !isZindexHigher) {
            el = el.parentElement;
            var zindex = getStyle(el,'zIndex');
            isZindexHigher = (zindex && zindex > 10);
        }

        return isZindexHigher;
    }

    function getLabelForElement(elem,labels) {
        if(!elem) {return null;}

        if(labels && labels.length) {

            for(var i = labels.length;i--;) {
                if(labels[i].htmlFor !== '') {
                    var el = document.getElementById(labels[i].htmlFor);
                    if (elem === el) {
                        var elementLabel = getInnerContent(labels[i]);
                        return trimStarFromEnd(elementLabel);
                    }
                }
            }
        }


        var elementParent = elem.parentElement;
        if(elementParent) {
            var parentnode = elementParent.nodeName;
            if(parentnode && parentnode === "TD") {
                var previousElement = elementParent.previousElementSibling;
                if(previousElement && previousElement.nodeName === "TD") {
                    var labelContent = getInnerContent(previousElement);
                    if(labelContent) {
                        return labelContent;
                    }
                }
            }
        }
        return null;
    }

    function isButton(field) {
        if(!field) {return false;}

        var fieldType = field.getAttribute('type') || field.type,
            fieldNode = field.nodeName.toLowerCase();

        if(!fieldType) {return false;}

        fieldType = fieldType.toLowerCase();

        if( (fieldNode === 'input' && (fieldType === "submit" || fieldType === "button" || fieldType === "image")) || (fieldNode === "button") ) {
            return true;
        }

        return false;
    }

    function getFieldsBasedOnType(fields,type) {
        if(!fields || fields.length === 0 || !type) {
            return [];
        }

        var list = [];

        for(var i = fields.length; i--;) {
            if(fields[i].htmlType === type) {
                list.push(fields[i]);
            }
        }

        return list.reverse();
    }

    function fieldsTypeCounter(fields,type) {
        var count = 0;
        for(var i = fields.length; i--;) {
            if(fields[i].htmlType === type) {
                count++;
            }
        }
        return count;
    }

    function containKeywords(attribute,list) {
        if(!attribute || (attribute && !attribute.trim()) || !list) {
            return false;
        }

        attribute = attribute.toLowerCase();

        for(var i = list.length; i--;) {
            if(attribute.indexOf(list[i]) !== -1) {
                return true;
            }
        }
        return false;
    }

    function isEqualToKeyword(attribute,list) {
        if(!attribute) {
            return false;
        }

        attribute = attribute.replace(/[`~!@#$%%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');
        attribute = attribute.trim().toLowerCase();

        for(var i = list.length; i--;) {
            if(attribute === list[i]) {
                return true;
            }
        }
        return false;
    }

    function trimStarFromEnd(content) {
        if(!content) {
            return null;
        }

        var contentLength = content.length;
        if(content[contentLength-1] === '*') {
            return content.slice(0,contentLength - 1);
        }
        return content;
    }

    function getInnerContent(element) {
        if(!element) {return null;}

        var htmlLabel = null;

        htmlLabel = element.textContent || element.innerText;

        if(!htmlLabel){ return null;}

        htmlLabel = htmlLabel.replace(/[`~!@#$%%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');
        htmlLabel = htmlLabel.replace(/\&lt;br\&gt;/gi,"\n").replace(/(&lt;([^&gt;]+)&gt;)/gi, "");
        htmlLabel = htmlLabel.replace(/\s/g, ' ');
        htmlLabel = htmlLabel.trim().toLowerCase();

        return htmlLabel;
    }

    var filterFieldDictByType = function(fieldDict) {

        if(!fieldDict || fieldDict.length === 0) {return null;}

        var inputFields = [],buttonFields = [];

        for(var i = fieldDict.length;i--;) {
            var dict= fieldDict[i],
                field = dict.pointer,
                fieldNode = field.nodeName.toLowerCase(),
                fieldType = field.getAttribute('type');
                var isButtonType = isButton(field);

            if(fieldNode === 'input' && fieldType !== 'radio' && fieldType !== 'checkbox' && !isButtonType) {
                inputFields.push(dict);
            }

            if(isButtonType) {
                buttonFields.push(dict);
            }
        }

        return {
            inputFields : inputFields.reverse(),
            buttonFields : buttonFields.reverse()
        };
    };

    var filterFieldDictByVisibility = function(fieldDict) {
        if(!fieldDict || fieldDict.length === 0) {return null;}

        var inputFields = [],buttonFields = [];

        var filterFields = {
            visibleFields : [],
            hiddenFields  : []
        };

        for(var i = fieldDict.length;i--;) {
            var dict= fieldDict[i],
                field = dict.pointer;

            if(isVisible(field)) {
                filterFields.visibleFields.push(dict);
            } else {
                filterFields.hiddenFields.push(dict);
            }
        }

        filterFields.visibleFields.reverse();
        filterFields.hiddenFields.reverse();

        return filterFields;
    };

    var filterOutFieldsByType = function (fields,type) {
         if(!fields || fields.length === 0 || !type) {
             return [];
         }

         var list = [];

         for(var i = fields.length; i--;) {
             if(fields[i].htmlType !== type) {
                  list.push(fields[i]);
             }
         }

         return list.reverse();
    };

    var filterOutCvvPasswords = function (fields) {
          if(!fields || (fields && fields.length === 0)) {return [];}

          var CVV_FIELD_LABELS = ["cvv","cvv2","cvc2","cvv number","verification code","security code"];
          var CVV_FIELD_CLASS = ["vcc","cardpincode","cv2_number","ccvnumber","ccvnum","cvvvnum","verifycardcode","cardverificationcode","dccvv","cccvv","security_code","security-code","cardsecuritycode","securitycode","cc.security","cvc_code","cvccode","cccode","cvv","cvv2","cvv2/cvc2","cvv/cvc","cardcsc"];

          var list = [];

          for(var i = fields.length; i--;) {
              var field = fields[i];
              var cvvScore = 0;
              if(field.htmlType === "password") {
                  cvvScore += (field.htmlPlaceholder && containKeywords(field.htmlPlaceholder,CVV_FIELD_LABELS)) ? -1000 : 0;
                  cvvScore += (field.htmlId && containKeywords(field.htmlId,CVV_FIELD_CLASS)) ?  -1000 : 0;
                  cvvScore += (field.htmlName && containKeywords(field.htmlName,CVV_FIELD_LABELS)) ?  -1000 : 0;
                  cvvScore += (field.htmlLength && field.htmlLength <=4) ?  -1000 : 0;

                 if(cvvScore === 0) {
                    list.push(fields[i]);
                 }
             } else {
                 list.push(fields[i]);
             }
          }

          return list.reverse();
    };

    var isLoginButton = function(buttonDict,returnScore,optionalKeywordList){
        if(!buttonDict) {return false;}

        var loginLabels = [],
            buttonLabel = buttonDict.htmlValue || buttonDict.imageAlt,
            isLoginContent = false,
            score = 0;

        if(optionalKeywordList && optionalKeywordList.length > 0) {
            loginLabels = optionalKeywordList;
        } else {
            loginLabels = SIGNIN_LABELS;
        }


        if(buttonLabel) {
            buttonLabel = buttonLabel.toLowerCase();
            score += isEqualToKeyword(buttonLabel,loginLabels) ? 100 : 0;

            if(score === 0) {
                var subLabels = ["log in to","sign in to","login to","signin to"];
                score += containKeywords(buttonLabel,subLabels) ? 50 : 0;
                score += containKeywords(buttonLabel,loginLabels) ? 50 : 0;
            }

            if(score > 0) { isLoginContent = true;}
        }

        if(!buttonLabel || !isLoginContent) {
            var loginBtnlabel = 'loginbtn;btnlogin;signinbtn;btnsignin;submit;next;ok;enter'.split(';'),
                loginkeywords = loginLabels.concat(loginBtnlabel);

            score = 0;

            score += (containKeywords(buttonDict.htmlId,loginkeywords)) ? 10 : 0;
            score += (containKeywords(buttonDict.htmlName,loginkeywords)) ? 10 : 0;
            score += (containKeywords(buttonDict.htmlClass,loginkeywords)) ? 10 : 0;
            score += (containKeywords(buttonDict.imageAlt,loginkeywords)) ? 10 : 0;
            score += (containKeywords(buttonDict.imageSrc,loginkeywords)) ? 10 : 0;

            if(score >= 10) {
                isLoginContent = true;
            }
        }

        if(returnScore) {
            return score;
        } else {
            return isLoginContent;
        }
    };

    var getPageForms = function() {
        var pageForms    = slice.call(document.forms) || slice.call(document.getElementsByTagName('form')) || [],
            visibleForms = [];

        if(pageForms.length > 150) {return [];}

        for(var i = pageForms.length; i--;) {
            var pageForm = pageForms[i];

            if(isVisible(pageForm)) {
                visibleForms.push(pageForm);
            }
        }

        return visibleForms.reverse();
    };

    var discardFormFields = function (array) {
            if(!array || array.length === 0) {return [];}
            var list = [];
            for(var i=array.length;i--;) {
                var field = array[i],type = field.getAttribute('type'),tag = field.tagName;
                if(tag === "INPUT" && (type !== 'checkbox') && (type !== 'radio')&& (type !== 'reset')&&(type!=='hidden')) {
                    list.push(field);
                }

            }
            return list.reverse();
    };

    var createPageFieldDict = function(inputFields){

        if(!inputFields) {
            inputFields = slice.call(document.querySelectorAll('input:not([type="checkbox"]):not([type="radio"]):not([type="reset"]),button')) || [];
        } else {
            inputFields = discardFormFields(inputFields);
        }

        var totalFields = inputFields.length;
        var dict = [];

        if(!inputFields || totalFields === 0) { return [];}

        for(var i = totalFields; i--;) {
            var field                 = inputFields[i];
            var fieldDict             = {};
            fieldDict.indexNo         = i;
            fieldDict.nodeName        = field.nodeName;
            fieldDict.imageAlt        = field.getAttribute('alt');
            fieldDict.imageSrc        = field.getAttribute('src');
            fieldDict.labelTag        = field.getAttribute("label-tag");
            fieldDict.labelData       = field.getAttribute("label-data") || field.getAttribute('data-label');
            fieldDict.ariaLabel       = field.getAttribute("label-aria") || field.getAttribute('aria-label');
            fieldDict.ariaHidden      = field.getAttribute("aria-hidden");
            fieldDict.htmlName        = field.getAttribute("name");
            fieldDict.htmlId          = field.getAttribute('id');
            fieldDict.htmlType        = (field.getAttribute('type') || field.type).toLowerCase();
            fieldDict.htmlPlaceholder = field.getAttribute('placeholder');
            fieldDict.htmlClass       = field.getAttribute('class');
            fieldDict.htmlValue       = field.value || getInnerContent(field);
            fieldDict.htmlLength      = field.getAttribute('maxlength') || field.maxlength;
            fieldDict.pointer         = field;
            fieldDict.form            = field.form;
            dict.push(fieldDict);
        }

        dict.reverse();
        return dict;
    };

    var getFormSpecificFields = function(dict,form) {
        if(!dict || dict.length === 0 || !form) { return [];}

        var formFieldArray = [];
        for(var i = 0; i < dict.length; ++i) {
            var fieldDict = dict[i];
            var fieldForm = fieldDict.form;
            if(fieldForm && fieldForm === form ) {
                formFieldArray.push(fieldDict);
            }
        }

        return formFieldArray;
    };

    var filterFieldDictByVisibilityAndType = function(fieldsDict) {
        if(!fieldsDict || fieldsDict.length === 0) {return null;}
        var fieldsByVisibility = filterFieldDictByVisibility(fieldsDict);

        if(!fieldsByVisibility) { return null;}

        var filteredFields = {
            visible : filterFieldDictByType(fieldsByVisibility.visibleFields),
            hidden  : filterFieldDictByType(fieldsByVisibility.hiddenFields)
        };

        return filteredFields;
    };

    var createFormDict = function(pageFields) {
        var pageForms = getPageForms();

        if(!pageForms || pageForms.length === 0) { return [];}

        var dict = [];

        for(var i =0;i < pageForms.length; i+=1) {
            var pageForm  = pageForms[i];
            if(pageForm.elements.length > 0) {
                var formFieldsDict = createPageFieldDict(pageForm.elements);
                var formFields     = filterFieldDictByVisibilityAndType(formFieldsDict);

                if(!formFields || !formFields.visible || formFields.visible.inputFields.length === 0) {
                    continue;
                }

                var formDict           = {};

                formDict.htmlId     = pageForm.getAttribute('id');
                formDict.htmlName   = pageForm.getAttribute('name');
                formDict.htmlClass  = pageForm.getAttribute('class');
                formDict.fieldsDict = formFields;
                formDict.methodExist = pageForm.getAttribute('method') ? true : false;
                formDict.pageFields = pageFields;

                var formAction = pageForm.getAttribute('action');

                if(formAction && formAction !== "#") {
                    formDict.actionExist = true;
                } else {
                    formDict.actionExist = false;
                }

                if(formFields) {

                    if(formFields.visible) {
                        formDict.vInputFields  = formFields.visible.inputFields || [];
                        formDict.vButtonFields = formFields.visible.buttonFields || [];
                    } else {
                        formDict.vInputFields  = [];
                        formDict.vButtonFields = [];
                    }

                    if(formFields.hidden) {
                        formDict.hInputFields  = formFields.hidden.inputFields || [];
                        formDict.hButtonFields = formFields.hidden.buttonFields || [];
                    } else {
                        formDict.hInputFields  = [];
                        formDict.hButtonFields = [];
                    }

                } else {

                    formDict.vInputFields  = [];
                    formDict.vButtonFields = [];
                    formDict.hInputFields  = [];
                }

                formDict.htmlLabels    = slice.call(pageForm.getElementsByTagName('label'));
                formDict.pointer       = pageForm;
                formDict.htmlLinks  =  slice.call(pageForm.getElementsByTagName('a')) || [];

                dict.push(formDict);
            }
        }

        return dict;
    };

    var containKeywordsInButtonList = function(buttonList,keywordsArray){
        if(!buttonList || buttonList.length === 0) {return false;}

        for(var i = buttonList.length;i--;) {
            var button = buttonList[i],
                buttonContent = null;

            if(button.pointer) {
                buttonContent = button.htmlValue;
            } else {
                buttonContent = getInnerContent(button);
            }

            if(buttonContent && containKeywords(buttonContent.toLowerCase(),keywordsArray)) {
                return true;
            }
        }

        return false;
    };

    var isLoginFormat = function(htmlForm) {

        if(!htmlForm || !htmlForm.vInputFields) {
            return false;
        }

        var vFields = htmlForm.vInputFields,
            hFields = htmlForm.hInputFields,
            textFields = fieldsTypeCounter(vFields,'text') + fieldsTypeCounter(vFields,'email') + fieldsTypeCounter(vFields,'number'),
            pswdFields = fieldsTypeCounter(vFields,'password'),
            hiddenPswdFields = fieldsTypeCounter(hFields,'password');


        if(pswdFields === 0 && hiddenPswdFields === 1) {
            textFields--;
            pswdFields++;
        }

        if(pswdFields === 1 && textFields === 1) {
            return true;
        }

        return false;
    };

    var filterOutCaptchaFields = function (fields) {
       if(!fields) {return [];}

       return fields.filter(function (field) {
          if(field.htmlId) {
             return field.htmlId.toLowerCase().indexOf("captcha") === -1;
          }

          if(field.htmlName) {
             return field.htmlName.toLowerCase().indexOf("captcha") === -1;
          }

          return true;
       });
    };

    var containFakePassword = function(formDict) {
      var hFields = formDict.hInputFields;
      var vFields = formDict.vInputFields;
      var hpswdField = null;

      var hPswdFields = getFieldsBasedOnType(hFields,"password");
      if(hPswdFields.length === 0 || hPswdFields.length > 1) {
          return false;
      }else {
          hpswdField = hPswdFields[0];
      }


      for(var i = 0; i < vFields.length; i+=1) {
          triggerEvent(vFields[i].pointer,'focus');
          if(isVisible(hpswdField.pointer) && !isVisible(vFields[i].pointer)) {
              return {
                  "originalPswd" : hpswdField,
                  "dependentField" : vFields[i]
              };
          }

      }

      return false;
    };

    var formScoreFunction = function(htmlForm) {
        var score = 0;

        if(!htmlForm) {return score;}


        var vFields = filterOutCaptchaFields(htmlForm.vInputFields);

        vFields = filterOutCvvPasswords(vFields);

        var passwordFieldArray = vFields ? getFieldsBasedOnType(vFields,"password") : [];
        var passwordFields = passwordFieldArray ? passwordFieldArray.length : 0;

        if(vFields.length < 2) {
          if(passwordFields === 0 || passwordFields > 1) { return -1000;}
        }

        if(passwordFields === 0) {

           if(document.domain.indexOf("mynycb.com")) {return -1000;}

           var fake_pair =  containFakePassword(htmlForm);
           if(fake_pair) {
             passwordFields = 1;
             passwordFieldArray.push(fake_pair.originalPswd);
           } else {
             return -1000;
           }

        } else if(passwordFields === vFields.length) {
            score += 10;
        }

        score += containKeywords(htmlForm.htmlId,SIGNIN_LABELS) ? 20 : 0;
        score += containKeywords(htmlForm.htmlName,SIGNIN_LABELS) ? 20 : 0;
        score += containKeywords(htmlForm.htmlClass,SIGNIN_LABELS) ? 10 : 0;

        score += containKeywords(htmlForm.htmlId,EXCLUDE_FORM) ? -20 : 0;
        score += containKeywords(htmlForm.htmlName,EXCLUDE_FORM) ? -20 : 0;
        score += containKeywords(htmlForm.htmlClass,EXCLUDE_FORM) ? -10 : 0;


        var buttonList = htmlForm.vButtonFields.concat(htmlForm.htmlLinks);

        if(containKeywordsInButtonList(buttonList,SIGNIN_LABELS)) {
            score += 50;
        } else {
            score += containKeywordsInButtonList(buttonList,SIGNIN_LABELS_SECONDRY) ? 25 : 0;
        }


        score += containKeywords(htmlForm.htmlId,SIGNUP_LABELS) ? -5 : 0;
        score += containKeywords(htmlForm.htmlName,SIGNUP_LABELS) ? -5 : 0;
        score += containKeywords(htmlForm.htmlClass,SIGNUP_LABELS) ? -5 : 0;
        score += containKeywordsInButtonList(buttonList,SIGNUP_LABELS) ? -5 : 0;


        if(score > 0 || (vFields.length === 2 && passwordFieldArray.length > 0 && (!passwordFieldArray[0].htmlLength || passwordFieldArray[0].htmlLength > 4))) {
            score += isLoginFormat(htmlForm) ? 20 : 0;
        }else {
          if(passwordFields === 1 && passwordFieldArray[0].htmlLength > 4) {
            score += isLoginFormat(htmlForm) ? 20 : 0;
          }
        }

        if(score > 0) {
            score += isZindexHigher(htmlForm.pointer) ? 50 : 0;
        }

        return score;
    };

    var getMaximumElement = function (elements, scoreFunc) {
        if (elements && elements.length > 0) {
            var maxScore   = 0,
                maxElement = null,
                length     = elements.length;

            for (var i = 0; i < length; ++i) {
                var element = elements[i];
                var score = scoreFunc(element);
                if (score > maxScore) {
                    maxScore = score;
                    maxElement = element;
                }
            }
            return maxElement;
        }
        return null;
    };

    var guessExceptionLoginForm = function(formDict) {
        var pageUrl = location.hostname + location.pathname;
        return null;
    };

    var findLoginAnchor = function(formDict,pswdField){
        var links = [];

        var login_link = 'signon;sign on;login;log in;log in securely;log on;login now;sign in;einloggen;登  录;登录;signin;continue;submit;enter;weiter;accès;вход;connexion;entrar;anmelden;accedi;valider;登录;लॉग इन करें;giriş'.split(';');

        if(formDict) {
            var form = formDict.pointer;
            links = formDict.htmlLinks || slice.call(form.getElementsByTagName('a'));
            if(!pswdField) { pswdField = identifyLoginFields(formDict).pswdField; }
        } else {
            links = slice.call(document.getElementsByTagName('a'));
        }

        var loginLinkList = [];
        var maxScore = 0;

        var total_links = links.length;
        if(total_links > 0) {
            for(var i = 0; i < total_links; i+=1) {
                var link = links[i];
                var linkText  = getInnerContent(link),
                    linkID    = link.getAttribute('id'),
                    linkClass = link.getAttribute('class'),
                    linkName  = link.getAttribute('name'),
                    linkImage = link.getElementsByTagName('img') || [],
                    score     = 0;

                if(linkText && containKeywords(linkText.toLowerCase(),EXCLUDE_LINKS)) {
                    continue;
                }

                score += (linkText && isEqualToKeyword(linkText.toLowerCase(),login_link)) ? 100 : 0;

                score += (linkID && containKeywords(linkID.toLowerCase(),SIGNIN_LABELS)) ? 10 : 0;
                score += (linkClass && containKeywords(linkClass.toLowerCase(),SIGNIN_LABELS)) ? 10 : 0;
                score += (linkName && containKeywords(linkName.toLowerCase(),SIGNIN_LABELS)) ? 10 : 0;

                if(linkImage.length > 0) {
                    score += (linkImage[0] && isEqualToKeyword(linkImage[0].alt,SIGNIN_LABELS)) ? 20 : 0;
                }

                if(score > 0 && score >= maxScore) {
                    maxScore = score;
                    loginLinkList.push({
                        htmlId : linkID,
                        htmlName : linkName,
                        htmlValue : linkText,
                        htmlClass : linkClass,
                        pointer : link
                    });
                }
            }

            var total_login_links = loginLinkList.length;
            if(total_login_links > 0) {
                if(pswdField) {
                    for(i = 0; i < total_login_links; i+=1) {
                        if(pswdField.pointer.compareDocumentPosition(loginLinkList[i].pointer) & Node.DOCUMENT_POSITION_FOLLOWING) {
                            return loginLinkList[i];
                        }
                    }

                } else {
                    return loginLinkList[total_login_links - 1];
                }
            } else {
                return null;
            }
        }
    };

    var shortListLoginAnchors = function () {
        var links = slice.call(document.getElementsByTagName('a'));

        var loginLinkList = [];

        var total_links = links.length;
        if(total_links > 0) {
            for(var i = 0; i < total_links; i+=1) {
                var link = links[i];
                var linkText  = getInnerContent(link),
                    linkID    = link.getAttribute('id'),
                    linkClass = link.getAttribute('class'),
                    linkName  = link.getAttribute('name'),
                    linkImage = link.getElementsByTagName('img') || [],
                    score     = 0;

                score += (linkID && containKeywords(linkID.toLowerCase(),SIGNIN_LABELS)) ? 10 : 0;
                score += (linkClass && containKeywords(linkClass.toLowerCase(),SIGNIN_LABELS)) ? 10 : 0;
                score += (linkName && containKeywords(linkName.toLowerCase(),SIGNIN_LABELS)) ? 10 : 0;

                if(linkImage.length > 0) {
                    score += (linkImage[0] && isEqualToKeyword(linkImage[0].alt,SIGNIN_LABELS)) ? 20 : 0;
                    score += (linkImage[0] && containKeywords(linkImage[0].src,SIGNIN_LABELS)) ? 20 : 0;
                } else {
                    score += (linkText && isEqualToKeyword(linkText.toLowerCase(),SIGNIN_LABELS)) ? 100 : -100;
                }

                if(score > 0) {
                    loginLinkList.push({pointer : link,score : score});
                }
            }
        }

        return loginLinkList;
    };

    var guessLoginLinkFromPswdField = function (loginLinkList,pswdField) {
        if(!loginLinkList || loginLinkList.length === 0) {return null;}

        var total_login_links = loginLinkList.length;
        var maxScore = 0;
        var loginAnchor = null;
        for(var i = 0; i < total_login_links; i+=1) {
            var loginLink = loginLinkList[i];
            if(pswdField.pointer.compareDocumentPosition(loginLink.pointer) & Node.DOCUMENT_POSITION_FOLLOWING) {
                if(loginLink.score > maxScore && isVisible(loginLink.pointer)) {
                    loginAnchor = loginLink;
                    maxScore = loginLink.score;
                }
            }
        }

        return loginAnchor;
    };

    var findLoginButton = function (buttonDict,keywords,pswdField) {
        if(!buttonDict || buttonDict.length === 0) {return null;}

        if(!keywords) {
            keywords = SIGNIN_LABELS;
        }

        var length = buttonDict.length;
        var maxScore = 0;
        var loginButton = null;
        for(var i = 0; i < length; i+=1) {
            var button = buttonDict[i];

            if(pswdField && !(pswdField.pointer.compareDocumentPosition(button.pointer) & Node.DOCUMENT_POSITION_FOLLOWING)) {
                continue;
            }

            var score = isLoginButton(button,true,keywords);
            if(score > maxScore) {
                maxScore = score;
                loginButton = button;
            }
        }

        return loginButton;
    };

    var guessLoginFields = function (filteredFields) {
        if(!filteredFields) { return null;}

        var loginButtons = [],
            linentLoginButtons = [],
            visible = filteredFields.visible,
            loginFieldsArray = [],
            visibleInputs = [];

        if(visible && visible.buttonFields && visible.buttonFields.length > 0) {
            var length = visible.buttonFields.length;
            for(var i =length;i--;) {
                var button = visible.buttonFields[i];

                if(isLoginButton(button)) {
                    loginButtons.push(button);
                }

                if(isLoginButton(button,false,SIGNIN_LABELS_SECONDRY)) {
                    linentLoginButtons.push(button);
                }
            }
            loginButtons = loginButtons.reverse();
            linentLoginButtons = linentLoginButtons.reverse();

        }



        if(visible && visible.inputFields) {
            visibleInputs = filterOutCaptchaFields(visible.inputFields);
        } else {
            return null;
        }

        var pswdFields = getFieldsBasedOnType(visibleInputs,"password");

        if(pswdFields.length > 0) {
            pswdFields = filterOutCvvPasswords(pswdFields);
        } else {

            var dynamicField = visibleInputs.filter(function (field) {
               if(field.htmlId && field.htmlId.toLowerCase() === "password") {return true;}
               if(field.htmlName && field.htmlName.toLowerCase() === "password") {return true;}
               if(field.htmlPlaceholder && field.htmlPlaceholder.toLowerCase() === "password") {return true;}

               var labels = document.getElementsByTagName('LABEL');
               if(labels) {
                 var fieldLabel = getLabelForElement(field.pointer,labels);
                 return (fieldLabel && fieldLabel.indexOf("password") !==-1);
               }

               return false;

            });

            if(dynamicField && dynamicField.length > 0) {
               pswdFields = dynamicField;
            } else {
               return null;
            }
        }


        var total_vfields = visibleInputs.length;

        if(pswdFields.length === 0 || total_vfields === pswdFields.length) {
            return null;
        }

        if(loginButtons.length === 0 && linentLoginButtons.length === 0) {
            var loginAnchorList = shortListLoginAnchors();
            var loginAnchor = null;

            if(visibleInputs.length > 2) {
                if(!loginAnchorList || loginAnchorList.length === 0) {return null;}

                var pswdField = null,
                    userField = null,
                    pswd_login_map = null,
                    max_login_score = 0;


                pswdFields.forEach(function(field){
                    var loginLink = guessLoginLinkFromPswdField(loginAnchorList,field);
                    if(loginLink && loginLink.score > max_login_score) {
                        pswd_login_map = {
                            field : field,
                            link : loginLink
                        };
                        max_login_score = loginLink.score;
                    }
                });

                if(pswd_login_map) {
                    pswdField = pswd_login_map.field;
                    loginAnchor = pswd_login_map.link;
                } else {
                    if(pswdFields.length === 1) {
                        pswdField = pswdFields[0];
                    }
                }

                if(pswdField){
                    for(var j = total_vfields;j--; ) {
                        var inputField = visibleInputs[j];
                        if(inputField.htmlType !== "password" && inputField.indexNo < pswdField.indexNo) { userField = inputField; break;}
                    }

                    var loginFields = {
                        parentForm : pswdField.form,
                        userField   : userField,
                        pswdField   : pswdField,
                        loginButton : loginAnchor 
                    };

                    loginFieldsArray.push(loginFields);
                }

            } else {
                if(pswdFields && pswdFields.length === 1) {
                    var pswdField = pswdFields[0];
                    var userField = null;
                    var total_login_links = loginAnchorList.length;
                    for(var k = total_vfields;k--; ) {
                        var inputField = visibleInputs[k];
                        if(inputField.htmlType !== "password" && inputField.indexNo < pswdField.indexNo) { userField = inputField; break;}
                    }

                    loginAnchor = guessLoginLinkFromPswdField(loginAnchorList,pswdField);

                    if(userField) {
                        loginFieldsArray.push({
                            parentForm : pswdField.form,
                            pswdField : pswdField,
                            userField : userField,
                            loginButton : loginAnchor 
                        });
                    } else {
                        loginFieldsArray.push({
                            parentForm : pswdField.form,
                            pswdField : pswdField,
                            loginButton : loginAnchor 
                        });
                    }
                }
            }
        } else {

            if(loginButtons.length === 0) {
                loginButtons = linentLoginButtons;
            }

            var total_login_btn = loginButtons.length;
            for(var i = 0; i <total_login_btn; ++i) {
                var loginbtn = loginButtons[i];
                var pswdIndexToBeSearched = loginbtn.indexNo - 1;
                var pswdField = null;
                for(var k = total_vfields;k--; ) {
                    var inputField = visibleInputs[k];
                    var passwordLabel = ((inputField.htmlId && inputField.htmlId === "password") || (inputField.htmlName && inputField.htmlName === 'password')|| (inputField.htmlPlaceholder && inputField.htmlPlaceholder.toLowerCase() === "password")) ? true :false;

                    if((inputField.htmlType === "password" || passwordLabel) && inputField.indexNo < loginbtn.indexNo) { pswdField = inputField; break;}
                }


                if(pswdField) {
                    var userField = null;
                    for(var j = total_vfields;j--; ) {
                        var inputField = visibleInputs[j];
                        if(inputField.htmlType !== "password" && inputField.indexNo < pswdField.indexNo) { userField = inputField; break;}
                    }


                    if(userField) {
                        var loginField = {
                            parentForm : pswdField.form,
                            userField : userField,
                            pswdField : pswdField,
                            loginButton : loginbtn 
                        };

                        loginFieldsArray.push(loginField);
                    } else {
                        var loginField = {
                            parentForm : pswdField.form,
                            pswdField : pswdField,
                            loginButton : loginbtn 
                        };

                        loginFieldsArray.push(loginField);
                    }
                }

            }
        }

        if(loginFieldsArray.length > 0) {
            return loginFieldsArray[0];
        } else {
            return null;
        }
    };

    var filterOutExtaFields = function (fieldDict) {
      if(!fieldDict) {return [];}
      if(fieldDict && fieldDict.length === 0) {return [];}

      var SEARCH_KEYWORDS = ["special offers","find","keywords","query","question","subscribe","subscribe-form","subscription","subscription_form","register email","searchform","search","search-form","newsletter","location"],
          filteredFields = [];


      for(var i = fieldDict.length; i--;) {
          var field = fieldDict[i];
          var score = 0;

          score += containKeywords(field.htmlId,SEARCH_KEYWORDS) ? 10 : 0;
          score += containKeywords(field.htmlName,SEARCH_KEYWORDS) ? 10 : 0;
          score += containKeywords(field.htmlPlaceholder,SEARCH_KEYWORDS) ? 10 : 0;

          if(score === 0) {
              filteredFields.push(field);
          }
      }

      return filteredFields.reverse();
    };

    var userFormScoringFunc = function (htmlForm) {
       var score = 0;

       if(!htmlForm) {return score;}

       var passwordFieldArray = vFields ? getFieldsBasedOnType(htmlForm.vInputFields,"password") : [];
       var passwordFields = passwordFieldArray ? passwordFieldArray.length : 0;

       if(passwordFields > 0) {return 0;}

       var vFields = filterOutCaptchaFields(htmlForm.vInputFields);


       vFields = (vFields.length === 1) ? filterOutExtaFields(vFields) : vFields;

       if(vFields.length === 1) {
           score += containKeywords(htmlForm.htmlId,SIGNIN_LABELS) ? 10 : 0;
           score += containKeywords(htmlForm.htmlName,SIGNIN_LABELS) ? 10 : 0;
           score += containKeywords(htmlForm.htmlClass,SIGNIN_LABELS) ? 5 : 0;

           if(score === 0) {
               score += containKeywords(htmlForm.htmlId,["mainform","usernameform"]) ? 5 : 0;
               score += containKeywords(htmlForm.htmlName,["mainform","usernameform"]) ? 5 : 0;
           }

           if(score > 0) {
               score += isZindexHigher(htmlForm.pointer) ? 50 : 0;
           }

           var buttonList = htmlForm.vButtonFields.concat(htmlForm.htmlLinks);

           if(containKeywordsInButtonList(buttonList,SIGNIN_LABELS)) {
               score += 50;
           } else {
               score += containKeywordsInButtonList(buttonList,SIGNIN_LABELS_SECONDRY) ? 25 : 0;
           }
       }

       return score;
    };

    var guessLoginLink = function (linklist,beforeField) {

      if(!linklist || (linklist && linklist.length === 0)) {
          linklist = slice.call(document.getElementsByTagName('a')) || [];
      }

      var total_links = linklist.length;
      var maxScore = 0;
      var loginlink = null;
      for(var i = 0; i < total_links; i+=1) {
          var link = linklist[i];
          var score = 0;

          var text = getInnerContent(link);

          if(containKeywords(text,EXCLUDE_LINKS)) {
              continue;
          }

          score += containKeywords(text,SIGNIN_LABELS) ? 10 : 0;

          if(score === 0) {
              score += containKeywords(text,["next"]) ? 7 : 0;

              if(score === 0) {
                  score += containKeywords(text,SIGNIN_LABELS_SECONDRY) ? 5 : 0;
              }
          }

          score += containKeywords(text,["btn","button"]) ? 2 : 0;

          if(beforeField) {
              if(beforeField.pointer) {
                  if(beforeField.pointer.compareDocumentPosition(link) & Node.DOCUMENT_POSITION_FOLLOWING) {
                      score += 5;
                  }
              } else {
                  if(beforeField.compareDocumentPosition(link) & Node.DOCUMENT_POSITION_FOLLOWING) {
                      score += 5;
                  }
              }
          }

          if(score > 0 && score > maxScore) {
              maxScore = score;
              loginlink = link;
          }


      }

      return loginlink;
    };

    var guessUserForm = function(filteredFields,formDictList) {

             if(!filteredFields) {
                 var pageFieldDict = createPageFieldDict();
                 if(pageFieldDict.length === 0){return null;}
                 filteredFields = filterFieldDictByVisibilityAndType(pageFieldDict);
                 if(!filteredFields || (filteredFields && !filteredFields.visible)) {return null;}
             }

             if(!formDictList) {
                 formDictList = createFormDict(filteredFields);
                 void 0;
             }

             var userForm = getMaximumElement(formDictList,userFormScoringFunc);
             void 0;

             var userField = null;

             if(userForm !== null && userFormScoringFunc(userForm) > 0) {

                userField = guessUsernameField(userForm.vInputFields);

                if(!userField) {
                    userField = userForm.vInputFields[0].htmlType !== 'password' ?  userForm.vInputFields[0] : null;
                }

                if(userField) {

                    var buttonFields = userForm.vButtonFields;
                    var loginButton = null;

                    if(buttonFields && buttonFields.length > 0) {
                        loginButton = findLoginButton(buttonFields,null,userField);
                        if(!loginButton) {
                            loginButton = findLoginButton(buttonFields,SIGNIN_LABELS_SECONDRY,userField);
                        }
                        void 0;
                    }

                    if(!loginButton) {
                        loginButton = guessLoginLink(userForm.htmlLinks,userField);
                        loginButton = {pointer : loginButton};
                    }

                    return { userField : userField,loginButton : loginButton};
                }


            } else {

                var vFields = null;

                if(filteredFields && filteredFields.visible) {
                    vFields = filteredFields.visible.inputFields;
                }

                if(vFields) {
                    vFields = filterOutExtaFields(vFields);

                    userField = guessUsernameField(vFields);
                    void 0;

                    if(userField) {

                        var buttonFields = filteredFields.visible.buttonFields;
                        var loginButton = null;

                        if(buttonFields && buttonFields.length > 0) {
                            loginButton = findLoginButton(buttonFields,null,userField);
                            if(!loginButton) {
                                loginButton = findLoginButton(buttonFields,SIGNIN_LABELS_SECONDRY,userField);
                            }
                            void 0;
                        }

                        if(!loginButton) {
                            loginButton = guessLoginLink(null,userField);
                            if(loginButton) {
                                loginButton = {pointer : loginButton};
                            }
                        }

                        return { userField : userField,loginButton : loginButton};
                    }

                }
            }
            
             return null;
        };

    var guessLoginForm = function(pageFieldsDict) {

          if(!pageFieldsDict) {
              pageFieldsDict = createPageFieldDict();
          }

          if(pageFieldsDict.length === 0) {return null;}

          var filteredPageFieldDict = filterFieldDictByVisibilityAndType(pageFieldsDict);

          if(!filteredPageFieldDict ||  (filteredPageFieldDict && !filteredPageFieldDict.visible)) {
              return null;
          }

          var formDictList = createFormDict(filteredPageFieldDict);

          var loginForm = getMaximumElement(formDictList,formScoreFunction);
          if(loginForm !== null && formScoreFunction(loginForm) > 0) {
              return loginForm;
          }

          if(!loginForm) {
              var exceptionForm = guessExceptionLoginForm(formDictList);
              if(exceptionForm) {
                  return exceptionForm;
              }

              var loginFields = guessLoginFields(filteredPageFieldDict);

              if(loginFields) {
                  return loginFields;
              } else {
                  var usernameForm = guessUserForm(filteredPageFieldDict,formDictList);
                  if(usernameForm) {
                      return usernameForm;
                  }
              }
          }
          return null;
    };

    var guessPasswordField = function (fieldDict) {
      if(!fieldDict || fieldDict.length === 0) {return null;}

      var length = fieldDict.length;
      var maxScore = 0;
      var pswdField = null;

      var PASSWORD_KEYWORDS = ["password","passwort","hasło","salasana"];
      var labels = document.getElementsByTagName('LABEL');

      for(var i = 0; i < length; i+=1) {
         var field = fieldDict[i];
         var score = 0;

         score += containKeywords(field.htmlId,PASSWORD_KEYWORDS) ? 10 : 0;
         score += containKeywords(field.htmlName,PASSWORD_KEYWORDS) ? 10 : 0;
         score += containKeywords(field.htmlPlaceholder,PASSWORD_KEYWORDS) ? 10 : 0;

         var elementLabel = getLabelForElement(field.pointer,labels);
         if(elementLabel) {
           score += containKeywords(elementLabel,PASSWORD_KEYWORDS) ? 50 : 0;
         }


         if(score > maxScore) {
             maxScore = score;
             pswdField = field;
         }
      }
      return pswdField;

  };

  var guessUsernameField = function (fieldDict,passwordIndex) {
      if(!fieldDict || fieldDict.length === 0) {return null;}

      var pswdFields = getFieldsBasedOnType(fieldDict,"password");

      if(pswdFields.length !== fieldDict.length) {
          fieldDict = filterOutFieldsByType(fieldDict,"password");
      }

      fieldDict = filterOutExtaFields(fieldDict);

      var length = fieldDict.length;

      var maxScore = 0;
      var usernameField = null;
      var USERNAME_KEYWORDS = ["username","loginname","user","usuari","identificación","uname","användar","käyttäjätunnus","userid","登录名","用户名","CPR-nummer","Social Security number","bruger-id","user id","registration number","customerid","online id","login id","personal id","customer number","registered email","account name","accountname","member number","membership number","profile number","profile id","profilenumber","profileid","custno","custid"];
      var labels = document.getElementsByTagName('LABEL');


      for(var i = length;i--;) {
         var field = fieldDict[i];
         var score = 0;

         if(passwordIndex && field.indexNo > passwordIndex) {
             continue;
         }

         score += containKeywords(field.htmlId,USERNAME_KEYWORDS) ? 10 : 0;
         score += containKeywords(field.htmlName,USERNAME_KEYWORDS) ? 10 : 0;
         score += containKeywords(field.htmlClass,USERNAME_KEYWORDS) ? 10 : 0;
         score += containKeywords(field.htmlPlaceholder,USERNAME_KEYWORDS) ? 10 : 0;

         var elementLabel = getLabelForElement(field.pointer,labels);
         if(elementLabel) {
           score += containKeywords(elementLabel,USERNAME_KEYWORDS) ? 10 : 0;
         }


         if(score > maxScore) {
             maxScore = score;
             usernameField = field;
         }
      }

      if(!usernameField && fieldDict.length === 1) {
          var score = 0;
          var targetField = fieldDict[0];
          score += targetField.htmlType === "email" ? 10 : 0;
          score += containKeywords(targetField.htmlId,SIGNIN_LABELS) ? 10 : 0;
          score += containKeywords(targetField.htmlName,SIGNIN_LABELS) ? 10 : 0;
          score += containKeywords(targetField.htmlClass,SIGNIN_LABELS) ? 10 : 0;
          score += containKeywords(targetField.htmlPlaceholder,SIGNIN_LABELS) ? 10 : 0;
          if(score > 0) {
              usernameField = targetField;
          }
      }
      return usernameField;
  };

  var identifyLoginFields = function (formDict) {

    var vFields = formDict.vInputFields;
    var fake_pair = containFakePassword(formDict);

    if(!vFields || vFields.length === 0) { return null;}

    var pswdField = null,
        temp_pswdField = null,
        username_index = null,
        userField = null;

    var total_vfields = vFields.length;

    if(fake_pair) {
        pswdField = fake_pair.originalPswd;
        username_index = fake_pair.dependentField.indexNo - 1;
    } else {
        pswdField = getFieldsBasedOnType(vFields,"password");
        if(pswdField && pswdField.length > 0) {
            if(pswdField.length > 1) {
                temp_pswdField = guessPasswordField(pswdField);
                if(temp_pswdField) {
                    pswdField = temp_pswdField;
                } else {
                    pswdField = pswdField[pswdField.length -1];
                }
            } else {
                pswdField = pswdField[0];
            }

            username_index = pswdField.indexNo - 1;
        } else {

            var dynamicField = vFields.filter(function (field) {
               if(field.htmlId && field.htmlId.toLowerCase() === "password") {return true;}
               if(field.htmlName && field.htmlName.toLowerCase() === "password") {return true;}
               if(field.htmlPlaceholder && field.htmlPlaceholder.toLowerCase() === "password") {return true;}

               var labels = document.getElementsByTagName('LABEL');
               if(labels) {
                 var fieldLabel = getLabelForElement(field.pointer,labels);
                 return (fieldLabel && fieldLabel.indexOf("password") !==-1);
               }

               return false;

            });

            if(dynamicField && dynamicField.length > 0) {
               pswdField = dynamicField[0];
            } else {
               return null;
            }

        }
    }

    if(total_vfields > 1) {

        userField = guessUsernameField(vFields,pswdField.indexNo);

        if(!userField) {
            for(var i = total_vfields;i--;) {
                var field = vFields[i];
                var fieldType = field.htmlType;
                var fieldIndex = field.indexNo;
                if(fieldIndex <= username_index) {
                    if (fieldType === 'text' || fieldType === 'email' || fieldType === 'number' || fieldType === "tel") {
                        userField = field;
                        break;
                    } else {
                        username_index--;
                    }
                }
            }
        }

    } else {
        var notVisibleFields = formDict.hInputFields.filter(function(inputDict){
            var field = inputDict.pointer;
            if(inputDict.htmlType !== "hidden") {
                return true;
            } else {
                return false;
            }
        });
        if(notVisibleFields && notVisibleFields.length > 0) {
            var length = notVisibleFields.length;
            for(var i = length;i--;) {
                var fieldDict = notVisibleFields[i];
                var fieldType = fieldDict.htmlType;
                if ((fieldType === 'text' || fieldType === 'email' || fieldType === 'number') && pswdField && (fieldDict.pointer.compareDocumentPosition(pswdField.pointer) & Node.DOCUMENT_POSITION_FOLLOWING) ) {
                    userField = fieldDict;
                    break;
                }
            }
        }
    }


    var loginFields = {};
    loginFields.userField = userField;
    loginFields.pswdField = pswdField;
    loginFields.dependentField = fake_pair.dependentField || false;

    if(formDict.vButtonFields.length > 0) {
        var loginbutton = findLoginButton(formDict.vButtonFields) || findLoginAnchor(formDict,loginFields.pswdField);
        loginFields.buttonField = loginbutton;
    } else {
        var pageFields = formDict.pageFields;
        if(pageFields && pageFields.visible) {
            var buttonFields = pageFields.visible.buttonFields;
            loginFields.buttonField = findLoginButton(buttonFields,null,pswdField);
        }

    }


    return loginFields;
  };

  var animationFill = function(field,data) {
      if(!data) {return false;}
      simulateKeyPress(field);
      field.value = data;
      simulateKeyBoardEvent(field);
  };

  var initiateFill = function(loginFields,clientData) {

    if(!loginFields) {return;}
    var userField = loginFields.userField;
    var pswdField = loginFields.pswdField;
    var dependentField = loginFields.dependentField;

    if(!userField && !pswdField) {
        return;
    }

    if(userField) {
        userField = userField.pointer;
        if(userField.hasAttribute('value') ) {userField.removeAttribute('value');}
        if(userField.hasAttribute('readonly')){ userField.readOnly = false;}
        animationFill(userField,clientData.username);
    }

    if (pswdField) {
        pswdField = pswdField.pointer;
        if(dependentField)
        {
            dependentField = dependentField.pointer;
            triggerEvent(dependentField,'focus');
        }
        animationFill(pswdField,clientData.password);
    }

  };

  var fixFormSubmit = function (loginForm) {
    if(location.hostname === "www.icloud.com") {
        var icloudBtn = document.getElementsByClassName('auth-submit') || [];
        if(icloudBtn[0]) {
            triggerEvent(icloudBtn[0],'mousedown');
            triggerEvent(icloudBtn[0],'mouseup');
            return true;
        }
    }
    return false;
  };

  var defaultLoginFill = function (loginForm,clientData) {
    if(!loginForm || !clientData) { return;}

    var loginFields = null;
    if(loginForm.hasOwnProperty('userField')) {
        loginFields = loginForm;
    } else {
        loginFields = identifyLoginFields(loginForm);
    }

    if(!loginFields) {return;}

    initiateFill(loginFields,clientData);

//    if(clientData.autosubmit_login && clientData.password) {
//        void 0;
//        setTimeout(function () {
//            if(fixFormSubmit(loginForm)) {
//                return;
//            } else if(loginFields.loginButton || loginFields.buttonField) {
//                void 0;
//                var btn_to_click = loginFields.loginButton ? loginFields.loginButton.pointer : loginFields.buttonField.pointer;
//                if(btn_to_click) {
//                    btn_to_click.click();
//                }
//
//            } else if(loginForm.pointer) {
//                void 0;
//                var formSubmitButton = loginForm.pointer.querySelectorAll('input[type=submit],button[type=submit],input[id=submit],button[id=submit],input[name=submit],button[name=submit]');
//                if(formSubmitButton && formSubmitButton.length === 1) {
//                  void 0;
//                  formSubmitButton[0].click();
//                }else {
//                  var buttons = loginForm.pointer.querySelectorAll('button,input[type=button]');
//                  if(buttons.length === 0) {
//                    var linkList = slice.call(loginForm.pointer.querySelectorAll('a')) || [];
//                    var loginLink = linkList.filter(function (link) {
//                       return isEqualToKeyword(link.textContent,SIGNIN_LABELS);
//                    });
//
//                    if(loginLink && loginLink.length>0) {
//                       loginLink[0].click();
//                       void 0;
//                       return;
//                    }
//                 } else if(buttons.length === 1) {
//                  }
//                  void 0;
//
//                  if(loginForm.actionExist) {
//                    triggerSubmitEvent(loginForm.pointer);
//                  }
//
//                }
//            }
//        },600);
//    }
  };


  var guessAndFillLoginForm = function (clientData) {
    var loginForm = guessLoginForm();


    void 0;

    if (loginForm) {
        defaultLoginFill(loginForm, clientData);
    }
  };

    var exports = {};
    exports.guessAndFillLoginForm = guessAndFillLoginForm;
    return exports;
})();


loginModule.guessAndFillLoginForm(user_data);
